package com.filesys.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Transcoder {

    public static String encode(String str) {
        return encode(str, false);
    }

    public static String encode(String str, boolean gzip) {
        String dst = "";
        if (str != null) {
            byte[] bytes = gzip ? gzip(str) : str.getBytes();
            dst = Base64.getEncoder().encodeToString(bytes);
        }
        return dst;
    }

    public static String decode(String str) {
        return decode(str, false);
    }

    public static String decode(String str, boolean gzip) {
        String dst = "";
        if (str != null) {
            try {
                byte[] bytes = Base64.getDecoder().decode(str);
                dst = gzip ? gunzip(bytes) : new String(bytes);
            } catch (Exception e) {}
        }
        return dst;
    }

    public static byte[] gzip(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return out.toByteArray();
    }

    public static String gunzip(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        String decompressed = null;
        try {
            in = new ByteArrayInputStream(bytes);
            ginzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {}
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {}
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {}
            }
        }
        return decompressed;
    }

    public static void main(String[] args) {
        String str = "D:\\Sublime Text Backups\\2016-12-15\\C\\Users\\zhaojianjun\\AppData\\Roaming\\Sublime Text 2\\Packages\\User";
        String str1 = encode(str);
//        String str2 = decode(str);
        String str3 = encode(str, false);
//        String str4 = decode(str, false);
        System.out.println("str = " + str);
        System.out.println("str1 = " + str1);
//        System.out.println("str2 = " + str2);
        System.out.println("str3 = " + str3);
        System.out.println("str4 = " + URLEncoder.encode(str));
//        System.out.println("str4 = " + str4);
        
    }
}
